library(mongolite)

Sys.setlocale(locale = "chinese")

get_data_mongo <- function(host, dbname, dbcoll, username="", password="") {
  if (host == "") host = "localhost"
  url = paste("mongodb://", host, sep="")
  if (username != "") {
    url = paste("mongodb://", username, ":", password, "@", host, "/", dbname, sep="")
  }
  m <- mongo(dbcoll, dbname, url)
  return(m$find())
}

get_data_csv <- function(name) {
  return(read.csv(name))
}

get_data <- function(fromdb = TRUE, host="", dbname="", dbcoll="", path = "", user = "", pwd = "") {
  if (fromdb) {
    return(get_data_mongo(host, dbname, dbcoll, user, pwd))
  } else {
    return(get_data_csv(paste(path, dbcoll, sep="/")))
  }
}

get_data_todayall <- function(fromdb = TRUE, host="", dbcoll="", path = "", user = "", pwd = "") {
  name <- paste("alltoday_", dbcoll, sep = "")
  if (!fromdb) name <- paste(name, ".csv", sep = "")
  return(get_data(fromdb, host, "stock", dbcoll = name, path, user, pwd))
}

## t: todaydata
todayall_add_level <- function(t) {
    t$level[t$changepercent >= 9.9] <- "9.11"
    t$level[t$changepercent >= 9 & t$changepercent < 9.9] <- "9.10"
    t$level[t$changepercent >= 8 & t$changepercent < 9] <- "9"
    t$level[t$changepercent >= 7 & t$changepercent < 8] <- "8"
    t$level[t$changepercent >= 6 & t$changepercent < 7] <- "7"
    t$level[t$changepercent >= 5 & t$changepercent < 6] <- "6"
    t$level[t$changepercent >= 4 & t$changepercent < 5] <- "5"
    t$level[t$changepercent >= 3 & t$changepercent < 4] <- "4"
    t$level[t$changepercent >= 2 & t$changepercent < 3] <- "3"
    t$level[t$changepercent >= 1 & t$changepercent < 2] <- "2"
    t$level[t$changepercent > 0 & t$changepercent < 1] <- "1"
    t$level[t$changepercent == 0] <- "0"
    t$level[t$changepercent >= -1 & t$changepercent < 0] <- "-K1"
    t$level[t$changepercent >= -2 & t$changepercent < -1] <- "-J2"
    t$level[t$changepercent >= -3 & t$changepercent < -2] <- "-I3"
    t$level[t$changepercent >= -4 & t$changepercent < -3] <- "-H4"
    t$level[t$changepercent >= -5 & t$changepercent < -4] <- "-G5"
    t$level[t$changepercent >= -6 & t$changepercent < -5] <- "-F6"
    t$level[t$changepercent >= -7 & t$changepercent < -6] <- "-E7"
    t$level[t$changepercent >= -8 & t$changepercent < -7] <- "-D8"
    t$level[t$changepercent >= -9 & t$changepercent < -8] <- "-C9"
    t$level[t$changepercent >= -9.9 & t$changepercent < -9] <- "-B10"
    t$level[t$changepercent <= -9.9] <- "-A11"
    return(t)
}

## statistical todaydata with level
## tt: todaydata
## return: statisticals
todayall_parse_level <- function(tt) {
  xx <- as.data.frame(table(tt$level))
  xx <- xx[order(as.numeric(xx$Var1)),]
  return(xx)
}

get_data_companies <- function(fromdb = TRUE, host="", user = "", pwd = "") {
  return(get_data(fromdb, host, "stock", "companies", user = user, pwd = pwd))
}

get_data_concepts <- function(fromdb = TRUE, host="", user = "", pwd = "") {
  return(get_data(fromdb, host, "stock", "concepts", user = user, pwd = pwd))
}

get_data_filter <- function(hc, concepts, concept, area, industry) {
  n_hc <- hc
  if (area != "All") n_hc <- hc[hc$area == area,]
  if (industry != "All") n_hc <- n_hc[n_hc$industry == industry,]
  new_hc <- n_hc[-seq(1, length(n_hc[,1])),]
  if (concept != "All") {
    n_con <- concepts[concepts$c_name == concept,]
    for (name in n_con$name) {
        new_hc <- rbind(new_hc, n_hc[n_hc$name == name,])
    }
  } else {
    new_hc <- n_hc
  }
  return(new_hc)
}

get_all_concepts <- function(concepts) {
  index <- duplicated(concepts$c_name)
  nc <- concepts[!index,]
  return(nc$c_name)
}

get_all_concepts_from <- function(concepts, hc) {
  len_con <- length(concepts[,1])
  c_new <- concepts[-seq(1,len_con),]
  for (name in hc$name) {
    tconcepts <- concepts[concepts$name == name,]
    c_new <- rbind(c_new, tconcepts)
  }
  return(get_all_concepts(c_new))
}

get_all_industry <- function(companies) {
  index <- duplicated(companies$industry)
  nc <- companies[!index,]
  return(nc$industry)
}

get_all_area <- function(companies) {
  index <- duplicated(companies$area)
  nc <- companies[!index,]
  return(nc$area)
}

## td: todaydata
## comp: companies
## bigger: which to parse
take_out_companies_high <- function(td, comp, bigger) {
  find_today <- td[td$changepercent > bigger,]
  for (name in find_today$name) {
    find_today$area[find_today$name == name] <- comp[comp$name == name,]$area
    find_today$industry[find_today$name == name] <- comp[comp$name == name,]$industry
  }
  return(find_today[order(find_today$changepercent, decreasing = TRUE),])
}

take_out_companies_low <- function(td, comp, lesser) {
  find_today <- td[td$changepercent < lesser,]
  for (name in find_today$name) {
    find_today$area[find_today$name == name] <- comp[comp$name == name,]$area
    find_today$industry[find_today$name == name] <- comp[comp$name == name,]$industry
  }
  return(find_today[order(find_today$changepercent, decreasing = TRUE),])
}

take_out_companies <- function(td, comp, higher, lesser) {
  find_today <- td[td$changepercent >= lesser & td$changepercent <= higher,]
  for (name in find_today$name) {
    v <- comp[comp$name == name,]
    if (length(v[,1]) > 0) {
      find_today$area[find_today$name == name] <- v$area
      find_today$industry[find_today$name == name] <- v$industry
    }
  }
  return(find_today[order(find_today$changepercent, decreasing = TRUE),])
}

draw_high_area <- function(hc_high) {
  x <- as.data.frame(table(hc_high$area))
  ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "High Industry") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title="Parse", x="Level", y="Count")
}

draw_low_area <- function(hc_low) {
  x <- as.data.frame(table(hc_low$area))
  ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "High Industry") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title="Parse", x="Level", y="Count")
}

draw_classify <- function(hc) {
  x <- as.data.frame(table(hc))
  colnames(x) <- c("Name", "Freq")
  ggplot(x, aes(x=Name, y=Freq), group = factor(1), main = "Classify") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Name), 
              show.legend = FALSE) +
    labs(title="Parse", x="Name", y="Count")
}

draw_todayall_ss <- function(todayss) {
  ccount <- length(todayss$Var1)
  cleft <- nrow(todayss[substring(todayss$Var1,0,1) == "-",])
  cright <- ccount - cleft - 1
  cat("today top count ", ccount, "cleft : ", cleft, ", cright : ", cright, "\n")
  
  ccolor <- c(rep("green", cleft), rep("white", 1), rep("red", cright))
  
  ggplot(todayss, aes(x=Var1, y=Freq), group = factor(1)) + 
    geom_bar(stat = "identity", width = 0.99, fill=ccolor) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title="DataParse", x="Level", y="Count")
}

